package com.it.park.controller.expose;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.client.service.NoticeService;
import com.it.park.common.Page;
import com.it.park.constant.Constant;
import com.it.park.model.Notice;

@Controller
@RequestMapping("/expose/notice")
public class ExposeNoticeController {
	private String rootPath=Constant.EXPOSE_PATH;

	@Autowired
	private NoticeService noticeService;
	
	private Page page = null;
	
	@RequestMapping("listRows")
	@ResponseBody
	public Map<String,Object> listRows(HttpServletRequest request){
			List<Notice> list = noticeService.selectAll();
			int count = list.size();
			Map<String,Object> map  =new HashMap<String, Object>();
			map.put("total", count);
			map.put("rows", list);
			return map;
	}
	
	@RequestMapping("list")
	public String list(@RequestParam("pageNow") Integer pageNow,HttpServletRequest request){
			List<Notice> notices =	null;
			Map<String,Object> map = new HashMap<String, Object>();
			Integer total = noticeService.selectTotal(map);
			if(pageNow != null){
				page = new Page(pageNow,total,10);
			}else{
				page = new Page(1,total,10);
			}
			map.put("startPos", page.getStartPos());
			map.put("pageSize", page.getPageSize());
			notices = noticeService.listRelUser(map);
			request.setAttribute("totals", total);
			request.setAttribute("page", page);
			request.setAttribute("notices", notices);
			
			return rootPath+"notice/noticeList";
	}
	
	/**
	 *单个通知详情 
	 */
	@RequestMapping("detail")
	public String detail(@RequestParam("id") Integer id,HttpServletRequest request){
		noticeService.updateClickRate(id);//每点击一次，加1
		Notice prevNotice = noticeService.getPrevById(id);//前一页
		Notice notice = noticeService.selectByPrimaryKey(id);//当前页
		Notice backNotice = noticeService.getBackById(id);//后一页
		request.setAttribute("notice", notice);
		request.setAttribute("prevNotice", prevNotice);
		request.setAttribute("backNotice", backNotice);
		return rootPath+"notice/noticeDetail";
	}
	
	
}
